//
//  NewsModel.m
//  News
//
//  Created by xalo on 16/2/26.
//  Copyright © 2016年 News. All rights reserved.
//

#import "NewsModel.h"

@implementation NewsModel

- (void)setValue:(id)value forKey:(NSString *)key {
    [super setValue:value forKey:key];
    
    if ([key isEqualToString:@"id"]) {
        
        [self setValue:value forKey:@"ID"];
    }
    
    if ([key isEqualToString:@"pics"]) {
        NSArray *array = value[@"list"];
        for (NSDictionary *dict in array) {
            
            if (dict[@"kpic"] != nil) {
                NSString *picUrl = dict[@"kpic"];
                [self.picsArray addObject:picUrl];

            }
        }
    }
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

//图片数组的懒加载
- (NSMutableArray *)picsArray {
    if (!_picsArray) {
        _picsArray = [NSMutableArray array];
    }
    return _picsArray;
}

// 归档方法
- (void)encodeWithCoder:(NSCoder *)aCoder {
    
    [aCoder encodeObject:_ID forKey:@"ID"];
    [aCoder encodeObject:_title forKey:@"title"];
    [aCoder encodeObject:_intro forKey:@"intro"];
    [aCoder encodeObject:_long_title forKey:@"long_title"];
    [aCoder encodeObject:_category forKey:@"category"];
    [aCoder encodeObject:_link forKey:@"link"];
    [aCoder encodeObject:_picsArray forKey:@"picsArray"];
    [aCoder encodeInteger:_articlePubDate forKey:@"articlePubDate"];
    [aCoder encodeObject:_kpic forKey:@"kpic"];
    
}

// 反归档方法
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super init];
    if (self) {
        
        self.ID = [aDecoder decodeObjectForKey:@"ID"];
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.intro = [aDecoder decodeObjectForKey:@"intro"];
        self.long_title = [aDecoder decodeObjectForKey:@"long_title"];
        self.category = [aDecoder decodeObjectForKey:@"category"];
        self.link = [aDecoder decodeObjectForKey:@"link"];
        self.picsArray = [aDecoder decodeObjectForKey:@"picsArray"];
        self.articlePubDate = [aDecoder decodeIntegerForKey:@"articlePubDate"];
        self.kpic = [aDecoder decodeObjectForKey:@"kpic"];
    }
    return self;
}


@end
